﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace Cookie_Hero.graphics
{
    public class ImageSequenceController
    {
        public int Lenght { get; private set; }
        bool playing = true;
        long currentMS = 0;
        int frameLength = 40;
        List<TextureArray> textures;
        ContentManager Content;

        public ImageSequenceController(ContentManager Content)
        {
            this.textures = new List<TextureArray>();
            this.Content = Content;
            this.Lenght = 0;
        }


        /* By updating the animations spring to next frames when necessairy */
        public bool update(long elapsedGameTime)
        {
            if (playing)
            {
                currentMS += elapsedGameTime;
                if (currentMS > frameLength)
                {
                    int updatesequences = (int)currentMS / frameLength;
                    foreach (TextureArray texture in textures)
                    {
                        for (int i = 0; i < updatesequences; i++)
                        {
                            texture.update();
                        }
                    }
                    currentMS = 0;
                    return true;
                }
            }
            return false;
            
        }

        /* Create a image sequence 
         * @param imageResources[]: The resouce names (extensions exluded, "tree.png" -> "tree"
         * @RETURN bool: true if creation succeeds
         */
        public bool createSequence(int sequenceId, String imageResource)
        {
            return createSequence(sequenceId, imageResource, null);
        }

        /* Create a image sequence 
         * @param imageResources[]: The resouce names (extensions exluded, "tree.png" -> "tree"
         * @param frameLength: Length of frames in MS
         * @RETURN bool: true if creation succeeds
         */
        public bool createSequence(int sequenceId, String imageResource, String name)
        {
            if (getTextureArrayByName(sequenceId) != null)
            {
                return false;
            }

            
            List<Texture2D> arrayTextures = new List<Texture2D>();

            // kijk of er xxxx achter staat en bepaal dan dat het 0000 0001 0002 etc loopt
            if (imageResource.Contains("xx"))
            {
                // kijk of de naam eindigt op xx13
                String[] imageResources;
                String[] letter = imageResource.Split('x');
                String count = "";
                String realName = "";
                int i = letter.Length - 1;
                while (!letter[i].Equals(""))
                {
                    count = letter[i] + count;
                    i--;
                }

                int j = 0;
                while (!imageResource.Substring(0, j).Contains("xx"))
                {
                    j++;
                }

                realName = imageResource.Substring(0, j - 2);

                int amountOfImages = Convert.ToInt32(count);
                imageResources = new String[amountOfImages];
                int counter = 0;

                while (counter < amountOfImages)
                {
                    String extraZeros;
                    if(counter < 10) {
                        extraZeros = "000";
                    }else if(counter < 100) {
                        extraZeros = "00";
                    }else if(counter < 1000) {
                        extraZeros = "0";
                    }else if(counter < 10000) {
                        extraZeros = "";
                    }else{
                        Log.E("It is not possible to have more than 9999 frames in the animation, sequence " + sequenceId + " failed to load!");
                        extraZeros = "";
                        return false;
                    }
                    try
                    {
                        Log.V("loaded texture " + realName + extraZeros + counter);
                        arrayTextures.Add(Content.Load<Texture2D>(realName + extraZeros + counter));
                    }
                    catch (ContentLoadException e)
                    {
                        Log.W("Failed to load texture " + realName + extraZeros + imageResources[0]);
                        return false;
                    }
                    counter++;
                }
            }
            else
            {
                arrayTextures.Add(Content.Load<Texture2D>(imageResource));
            }

            textures.Add(new TextureArray(sequenceId, arrayTextures));
            Lenght++;
            return true;
        }

        /* Return the specific texture you requested from the right image sequence */
        public Texture2D getSequenceImage(int sequenceId, int frame)
        {
            TextureArray textureArray = getTextureArrayByName(sequenceId);
            if (textureArray == null)
            {
                return null;
            }
            return textureArray.getTextureByFrame(frame);
        }

        /* Return the specific texture corresponding to the animation from the right image sequence */
        public Texture2D getSequenceImage(int sequenceId)
        {
            TextureArray textureArray = getTextureArrayByName(sequenceId);
            if (textureArray != null)
            {
                return textureArray.getTexture();
            }
            return null;
        }


        /* select the requested TextureArray */
        public TextureArray getTextureArrayByName(int sequenceId)
        {
            /*foreach (TextureArray texture in textures)
            {
                if (texture.id == sequenceId)
                {
                    return texture;
                }
            }*/
            int requestedTextureIndex = sequenceId - 1;
            if (requestedTextureIndex < Lenght)
            {
                return textures[requestedTextureIndex];
            }
            return null;
        }
    }
}
